package com.zadtek.jpsqishi.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.jpsqishi.R;
import com.zadtek.jpsqishi.activity.common.Common;
import com.zadtek.jpsqishi.activity.common.Const;
import com.zadtek.jpsqishi.activity.data.QiangDanData;
import com.zadtek.jpsqishi.frame.JsonProcessHelper;

/**
 * 申请提现
 * 
 * @author wwl
 * 
 */

public class MoneyTowithdrawal extends BaseActivity implements OnClickListener {
	ImageView backid;
	TextView topview; // 标题
	TextView money_drawal, money_account, money_OK;// 提现金额，银行账户，确定申请提现
	EditText money_number;
	private Handler mHandler;
	private SharedPreferences sp;
	private String account;
	private String uid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.moneydrawal);

		backid = (ImageView) findViewById(R.id.backid);
		topview = (TextView) findViewById(R.id.topview);
		money_drawal = (TextView) findViewById(R.id.money_drawal);
		money_account = (TextView) findViewById(R.id.money_account);
		money_OK = (TextView) findViewById(R.id.money_OK);
		money_number = (EditText) findViewById(R.id.money_number);

		topview.setText("申请提现");
		backid.setOnClickListener(this);
		money_OK.setOnClickListener(this);

		initViews();

		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case Const.STATE_START_ERROR:
					Common.DisplayToast(MoneyTowithdrawal.this, "通信失败,请检查网络!",
							2);
					break;
				case Const.STATE_GETNUM_SUCC: // 获取信息成功
					QiangDanData countdata = new QiangDanData();
					countdata = (QiangDanData) msg.obj;
					if (null != countdata) {
						Common.DisplayToast(MoneyTowithdrawal.this,
								countdata.getMessage(), 2);
						if (countdata.getStatus().equals("1")) {
							money_number.setText("");
							MoneyTowithdrawal.this.finish();
						} else {
							money_number.setText("");
						}
					}
					break;
				}
			}
		};
	}

	// 初始化界面信息
	void initViews() {
		sp = getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
		uid = sp.getString("uid", "");
		String account = sp.getString("account", "");
		String back = sp.getString("back", "");
		money_drawal.setText("每次至少提现50元，当前可提现余额" + account + "元");
		if (back != null && !back.equals("")) {
			back = back.equals("null") ? "" : back;
		}
		money_account.setText(back);

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.backid:
			finish();
			break;
		case R.id.money_OK:
			String money = money_number.getText().toString();
			if (money.equals("")) {
				Common.DisplayToast(MoneyTowithdrawal.this, "请输入提现金额！", 2);
				return;
			}
			// 获取信息
			if (Common.isNetworkConnected(MoneyTowithdrawal.this)) {
				new OrderAsyncTask().execute("getqsmsg", uid, money);
			}

			break;

		default:
			break;
		}
	}

	class OrderAsyncTask extends AsyncTask<String, String, String> {

		private QiangDanData countdata;

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String res = "";
			if (params[0].equals("getqsmsg")) {//申请提现
				countdata = JsonProcessHelper.jsonProcess_getDeposit(
						"set_emp_deposit", params[1], params[2]);
				if (countdata != null) {
					res = "state_success";
				} else {
					res = "mi_error";
				}
			}
			return res;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result.equals("state_success")) {
				Message msg = Message.obtain();
				msg.what = Const.STATE_GETNUM_SUCC;
				msg.obj = countdata;
				mHandler.sendMessage(msg);
			} else if (result.equals("ping_me_error")) {
				Message msg = Message.obtain();
				msg.what = Const.STATE_START_ERROR;
				mHandler.sendMessage(msg);
			}
		}
	}
}
